import bb.cascades 1.0
import "../chart/chartdata.js" as Chelp

Page {
    id: history_detail
    
    property string history_mode
    property date start
    property date end
    
    function setHistory(mode, data, st, en){
        arrayModel.clear();
        start = st;
        end = en;
        if (mode == "income"){
            history_mode = "income" ;
            for (var i=0; i<data.length; i++){
                arrayModel.append(data[i]);
            }
        } else if (mode == "expenses"){
            history_mode = "expenses" ;
            for (var i=0; i<data.length; i++){
                arrayModel.append(data[i]);
            }
        }
    }
    
    function update(){
        if (history_mode == "expenses"){
            var history_rt = database.GetAllPayment(dtp_start.value, dtp_end.value, 0);
//            console.log("expenses history_rt : " + JSON.stringify(history_rt));
            history_detail.setHistory("expenses", history_rt, dtp_start.value, dtp_end.value);
        }else if (history_mode == "income"){
            var history_rt = database.GetAllIncome(dtp_start.value, dtp_end.value, 0);
//            console.log("income history_rt : " + JSON.stringify(history_rt));
            history_detail.setHistory("income", history_rt, dtp_start.value, dtp_end.value);
        }
//        arrayModel.value(inx).FAMOUNT = am;
//        console.log("arrayModel.value(inx) : " + JSON.stringify(arrayModel.value(inx)));
//        if (history_mode == "income"){
//            arrayModel.value(inx).FITYPEID = tid;
//        }else if (history_mode == "expenses"){
//            arrayModel.value(inx).FPTYPEID = tid;
//        }
    }
    
    paneProperties: NavigationPaneProperties {
                backButton: ActionItem {
                    onTriggered: {
                        // define what happens when back button is pressed here
                        // in this case is closed the detail page
                        historyNavigation.pop()
                    }
                }
            }
    
    actions: [
        // define the actions for first tab here
//        ActionItem {
//            title: qsTr("Rotate")
//            onTriggered: {
//                //                imgTab1.rotationZ = imgTab1.rotationZ + 90;
//            }
//        },
//        ActionItem {
//            title: qsTr("Break")
//            onTriggered: {
//                //                imgTab1.imageSource = "asset:///images/picture1br.png";
//            }
//        }
    ]
    
    titleBar: TitleBar {
        id: titleBar
        visibility: ChromeVisibility.Visible
        title: lang.history_title
    }
    
    Container {
        ListView {
            property alias mode: history_detail.history_mode
            property string lamount: lang.history_update_amount
            property string ltype: lang.history_detail_type
            
            dataModel: ArrayDataModel {
                id: arrayModel
            }
            
            //            onCreationCompleted: {
            //                for ( var a = 0; a < 20; a++ ) {
            //                    arrayModel.append({"amount":"amount " + a, "type":"type "+a, "time":new Date()});
            //                }
            //                //                arrayModel.append(["Appended 1", "Appended 2"]);
            //                //                arrayModel.removeAt(0);
            //                //                arrayModel.insert(0,["Prepended 1", "Prepended 2"]);
            //            }
            
            
            // Add appearance definitions for the list items using the
            // listItemComponents list
            listItemComponents: [
                ListItemComponent {
                    StandardListItem {
//                        minHeight: 200
                        title: ListItem.view.lamount + ListItemData.FAMOUNT
                        description: {
                            if (ListItem.view.mode == "expenses"){
//                                console.log("ListItem.view.mode : " + ListItem.view.mode);
                                ListItem.view.ltype + Chelp.ptypeName(ListItemData.FPTYPEID) ;
                            }else if (ListItem.view.mode == "income"){
//                                console.log("ListItem.view.mode : " + ListItem.view.mode);
                                ListItem.view.ltype + Chelp.itypeName(ListItemData.FITYPEID) ;
                            }
                        }
                        status: {
                            if (ListItem.view.mode == "expenses"){
                                ListItemData.FDATE
                            }else if (ListItem.view.mode == "income"){
                                ListItemData.FDate
                            }
                        }
                    }
                } // end of second ListItemComponent
            // The first ListItemComponent defines how "header" items
            // should appear. These items use a Label.
            //                ListItemComponent {
            //                    type: "header"
            //                    Label {
            //                        text: ListItemData.title
            //                        
            //                        // Apply a text style to create a large, bold font with
            //                        // a specific color
            //                        textStyle {
            //                            base: SystemDefaults.TextStyles.BigText
            //                            fontWeight: FontWeight.Bold
            //                            color: Color.create ("#7a184a")
            //                        }
            //                    }
            //                },
            
            // The second ListItemComponent defines how "listItem" items
            // should appear. These items use a Container that includes a
            // CheckBox and a Label.
            //                ListItemComponent {
            //                    type: "listItem"
            //                    Container {
            //                        layout: StackLayout {
            //                            orientation: LayoutOrientation.LeftToRight
            //                        }
            //                        CheckBox {
            //                            // Determine whether the CheckBox should be checked
            //                            // according to a value in the data model
            //                            checked: ListItemData.checked
            //                        }
            //                        Label {
            //                            text: ListItemData.title
            //                            
            //                            // Apply a text style to create a title-sized font
            //                            // with normal weight
            //                            textStyle {
            //                                base: SystemDefaults.TextStyles.TitleText
            //                                fontWeight: FontWeight.Normal
            //                            }
            //                        }
            //                    } // end of Container
            //                } // end of second ListItemComponent
            ] // end of listItemComponents list
            
            onTriggered: {
                console.log("indexPath : " + JSON.stringify(indexPath));
                var chosenItem = arrayModel.data(indexPath);
                console.log("chosenItem : " + JSON.stringify(chosenItem));
                var ti;
                var tid;
                if (history_detail.history_mode == "income"){
                    ti = chosenItem.FDate;
                    tid = chosenItem.FITYPEID;
                }else if (history_detail.history_mode == "expenses"){
                    ti = chosenItem.FDATE;
                    tid = chosenItem.FPTYPEID;
                }
                
                var updatePage = historyUpdateDefinition.createObject();
                updatePage.setHistory(history_detail.history_mode, chosenItem.fid, chosenItem.FAMOUNT, ti, tid, indexPath[0], chosenItem.FDESCRIPTION);
                
                // Finally push the new Page.
                historyNavigation.push(updatePage);
            }
            
        } // end of ListView
    
    }
    
    attachedObjects: [
        ComponentDefinition {
            id: historyUpdateDefinition
            source: "HistoryUpdate.qml"
        }
    ]
//        property Page historyDetailPage
//        function getHistoryDetailPage() {
//            //        if (! addDetailPage) {
//            historyDetailPage = historyDetailDefinition.createObject();
//            //        }
//            return historyDetailPage;
//        }
}
